<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("db.inc.php");
use_unit("dbtables.inc.php");
use_unit("components4phpfull/jtmenubar.inc.php");
use_unit("components4phpfull/jtmenu.inc.php");
use_unit("components4phpfull/jtdatetimepicker.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jthorizontalline.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Create extends Page
{
       public $edCapacity = null;
       public $Label14 = null;
       public $JTDivInfo = null;
       public $JTGroupBox5 = null;
       public $Label13 = null;
       public $imgInfo = null;
       public $JSMakeModel = null;
       public $lbltrack = null;
       public $lblHours = null;
       public $lblOdometer = null;
       public $Label12 = null;
       public $hfError = null;
       public $JSValidate = null;
       public $lblplate = null;
       public $lblyr = null;
       public $Label2 = null;
       public $edcustomer = null;
       public $edhours = null;
       public $Label11 = null;
       public $Label10 = null;
       public $Label9 = null;
       public $hiddenCompanyName = null;
       public $Button1 = null;
       public $cmbownership = null;
       public $cmbpersonnel = null;
       public $edOwnership = null;
       public $listOwnership = null;
       public $btnAddOwnership = null;
       public $btnDelOwnership = null;
       public $JTJavaScript12 = null;
       public $JTJavaScript11 = null;
       public $JTJavaScript10 = null;
       public $JTDivOwnership = null;
       public $JTGroupBox4 = null;
       public $btnShowOwnership = null;
       public $edother = null;
       public $Label50 = null;
       public $cmbsize = null;
       public $Label8 = null;
       public $JTJavaScript9 = null;
       public $JTJavaScript8 = null;
       public $JTJavaScript7 = null;
       public $JTDivDept = null;
       public $btnDelDept = null;
       public $btnAddDept = null;
       public $edDept = null;
       public $listDept = null;
       public $JTGroupBox3 = null;
       public $btnShowDept = null;
       public $cmbDept = null;
       public $uploadEmp = null;
       public $lblPhoto = null;
       public $edCurrOd = null;
       public $lblCurrOd = null;
       public $edodometer = null;
       public $lblOd = null;
       public $cmbtrack = null;
       public $lbltrackby = null;
       public $Label52 = null;
       public $JTDivType = null;
       public $JTGroupBox1 = null;
       public $btnAddType = null;
       public $btnDelType = null;
       public $edType = null;
       public $listType = null;
       public $cmbvehop = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $btnDetails = null;
       public $JTSiteTheme1 = null;
       public $btnShowType = null;
       public $btnShowGarage = null;
       public $JTDivGarage = null;
       public $JTGroupBox2 = null;
       public $btnAddGarage = null;
       public $btnDelGarage = null;
       public $edGarage = null;
       public $listGarage = null;
       public $compid = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $cmbclass = null;
       public $Label6 = null;
       public $Label5 = null;
       public $hiddenNewCount = null;
       public $hiddenExist = null;
       public $edChassis = null;
       public $Label4 = null;
       public $Memo1 = null;
       public $imgBack = null;
       public $Image2 = null;
       public $edEngine = null;
       public $lblengine = null;
       public $hiddenvehnum = null;
       public $hiddenSaveHere = null;
       public $JTMenu1 = null;
       public $JTMenuBar1 = null;
       public $lblcustom1 = null;
       public $cmblocation = null;
       public $lbldept = null;
       public $lbllocation = null;
       public $edvin = null;
       public $cmbmodel = null;
       public $cmbmanufacturer = null;
       public $cmbtype = null;
       public $edyear = null;
       public $edveh = null;
       public $Label7 = null;
       public $Label3 = null;
       public $lblownership = null;
       public $lbltype = null;
       public $lblmnfr = null;
       public $lblmodel = null;
       public $Label1 = null;
       function imgInfoJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivInfo').Show();
       <?php

       }

       function cmbtrackJSBlur($sender, $params)
       {

       ?>
       var val = document.getElementById('cmbtrack').value;
       if(val=='--Select--')
       {
       document.getElementById('lbltrack').innerHTML = '&darr This field is required &darr';
       }
       if(val!='--Select--')
       {
       document.getElementById('lbltrack').innerHTML = '';
       }
       <?php

       }

       function edhoursJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edhours','lblHours');
       <?php

       }

       function edodometerJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edodometer','lblOdometer');
       <?php

       }

       function edvehJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edveh','lblplate');

       var edveh = document.getElementById('edveh').value;

       if(edveh.length>0)
       {
       regex_pattern = "^[a-zA-Z]{2,3}[0-9]{3,4}$";
       var re = new RegExp(regex_pattern);
       var match = document.getElementById('edveh').value.match(re);
       if(match==null)
       {
        document.getElementById('lblplate').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(ZTE168)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edyearJSBlur($sender, $params)
       {

       ?>
       validateRequire('edyear','lblyr');
       <?php

       }

       function Vehicle_CreateJSLoad($sender, $params)
       {
       ?>
       document.getElementById('edcustomer_outer').style.visibility = 'hidden';

       //for ownership
       var comp = document.getElementById('hiddenCompanyName').value;
       var customer = document.getElementById('edcustomer_outer');
       var personnel = document.getElementById('cmbpersonnel_outer');
       var ownership = document.getElementById('cmbownership').value;

       if(ownership=='staff')
       {
       document.getElementById('edcustomer_outer').style.visibility = 'hidden';
       customer.style.left = '632px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'visible';
       personnel.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'visible';
       }

       if(ownership=='company')
       {
       personnel.style.left = '632px';
       customer.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'hidden';
       document.getElementById('edcustomer_outer').style.visibility = 'visible';
       document.getElementById('edcustomer').value = document.getElementById('hiddenCompanyName').value;
       }

       if(ownership=='leased' || ownership=='outsource')
       {
       personnel.style.left = '632px';
       customer.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'hidden';
       document.getElementById('edcustomer_outer').style.visibility = 'visible';
       document.getElementById('edcustomer').value = '';
       }

       //for tracking
       var tracking = document.getElementById("cmbtrack").value;
        if(tracking=='distance')
        {
        document.getElementById("edhours").disabled=true;
        document.getElementById("edhours").value='';
        document.getElementById("edhours").style.backgroundColor='#707070';
        document.getElementById("edodometer").style.backgroundColor='#000000';
        document.getElementById("edodometer").disabled=false;
        }
        if(tracking=='hours')
        {
        document.getElementById("edodometer").disabled=true;
        document.getElementById("edodometer").value='';
        document.getElementById("edodometer").style.backgroundColor='#707070';
        document.getElementById("edhours").style.backgroundColor='#000000';
        document.getElementById("edhours").disabled=false;
        }
        if(tracking=='--Select--')
        {
        document.getElementById("edhours").disabled=true;
        document.getElementById("edodometer").disabled=true;
        document.getElementById("edhours").value='';
        document.getElementById("edodometer").value='';
        document.getElementById("edhours").style.backgroundColor='#707070';
        document.getElementById("edodometer").style.backgroundColor='#707070';
        }

       makeModel();
       <?php

       }

       function Button1JSClick($sender, $params)
       {

       ?>

       alert(document.getElementById('hiddenCompanyName').value);
       document.getElementById('edcustomer_outer').style.left = '100px';
       alert('asd');
       <?php

       }

       function cmbownershipJSChange($sender, $params)
       {
       ?>
       //Add your javascript code here
       var comp = document.getElementById('hiddenCompanyName').value;
       var customer = document.getElementById('edcustomer_outer');
       var personnel = document.getElementById('cmbpersonnel_outer');
       var ownership = document.getElementById('cmbownership').value;

       if(ownership=='staff')
       {
       document.getElementById('edcustomer_outer').style.visibility = 'hidden';
       customer.style.left = '632px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'visible';
       personnel.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'visible';
       }

       if(ownership=='company')
       {
       personnel.style.left = '632px';
       customer.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'hidden';
       document.getElementById('edcustomer_outer').style.visibility = 'visible';
       document.getElementById('edcustomer').value = document.getElementById('hiddenCompanyName').value;
       }

       if(ownership=='leased' || ownership=='outsource')
       {
       personnel.style.left = '632px';
       customer.style.left = '113px';
       document.getElementById('cmbpersonnel_outer').style.visibility = 'hidden';
       document.getElementById('edcustomer_outer').style.visibility = 'visible';
       document.getElementById('edcustomer').value = '';
       }

       return false;
       <?php
       }

       function btnDelOwnershipJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var OwnershipIndex = document.getElementById('listOwnership').selectedIndex;
       var OwnershipValue = document.getElementById('listOwnership')[OwnershipIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxOwnership3(OwnershipValue,comp);
       <?php

       }

       function btnAddOwnershipJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addownership = document.getElementById('edOwnership').value;
       var comp = document.getElementById('compid').value;
       AjaxOwnership2(addownership,comp);
       document.getElementById('edOwnership').value = "";
       <?php

       }

       function btnShowOwnershipJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivOwnership').Show();
       document.getElementById('edOwnership').Show();
       <?php

       }

       function cmbsizeJSChange($sender, $params)
       {
          echo $this->cmbsize->ajaxCall("size");
          ?>
          //Add your javascript code here
          return false;
          <?php
       }

       function btnDelDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DeptIndex = document.getElementById('listDept').selectedIndex;
       var DeptValue = document.getElementById('listDept')[DeptIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDept3(DeptValue,comp);
       <?php

       }

       function btnAddDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddept = document.getElementById('edDept').value;
       var comp = document.getElementById('compid').value;
       AjaxDept2(adddept,comp);
       document.getElementById('edDept').value = "";
       <?php

       }




       function btnShowDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDept').Show();
       document.getElementById('edDept').value = "";
       <?php

       }


       function cmbmanufacturerJSChange($sender, $params)
       {
        //echo $this->cmbmanufacturer->ajaxCall('cmbmanufacturerChange');
        ?>
        makeModel();
        return false;
        <?php
       }

       function cmbtrackJSChange($sender, $params)
       {
        ?>
        var tracking = document.getElementById("cmbtrack").value;
        if(tracking=='distance')
        {
        document.getElementById("edhours").disabled=true;
        document.getElementById("edhours").value='';
        document.getElementById("edhours").style.backgroundColor='#707070';
        document.getElementById("edodometer").style.backgroundColor='#000000';
        document.getElementById("edodometer").disabled=false;
        }
        if(tracking=='hours')
        {
        document.getElementById("edodometer").disabled=true;
        document.getElementById("edodometer").value='';
        document.getElementById("edodometer").style.backgroundColor='#707070';
        document.getElementById("edhours").style.backgroundColor='#000000';
        document.getElementById("edhours").disabled=false;
        }
        if(tracking=='--Select--')
        {
        document.getElementById("edhours").disabled=true;
        document.getElementById("edodometer").disabled=true;
        document.getElementById("edhours").value='';
        document.getElementById("edodometer").value='';
        document.getElementById("edhours").style.backgroundColor='#707070';
        document.getElementById("edodometer").style.backgroundColor='#707070';
        }

        return(false);
        <?php

       }

       function btnDetailsJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.open("veh_details.php","Window1",  "menubar=no,width=1079,height=850,left=200,top=230,toolbar=no scroll=yes");
       <?php

       }


       function btnDelGarageJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var dropdownIndex = document.getElementById('listGarage').selectedIndex;
       var dropdownValue = document.getElementById('listGarage')[dropdownIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxGarage3(dropdownValue,comp);
       <?php

       }

       function btnAddGarageJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addgarage = document.getElementById('edGarage').value;
       var comp = document.getElementById('compid').value;
       AjaxGarage2(addgarage,comp);
       document.Vehicle_Create.edGarage.value = "";
       <?php

       }

       function btnShowGarageJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivGarage').Show();
       document.Vehicle_Create.edGarage.value = "";
       <?php

       }

       function btnDelTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var dropdownIndex = document.getElementById('listType').selectedIndex;
       var dropdownValue = document.getElementById('listType')[dropdownIndex].value;
       var compider3 = document.getElementById('compid').value;
       AjaxMothercomp3(dropdownValue,compider3);
       <?php

       }

       function btnAddTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addtype = document.getElementById('edType').value;
       var compider = document.getElementById('compid').value;
       AjaxMothercomp2(addtype,compider);
       document.Vehicle_Create.edType.value = "";
       <?php

       }



       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivType').Show();
       document.Vehicle_Create.edType.value = "";
       <?php

       }


       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }



       function btnCancelClick($sender, $params)
       {
        redirect('vehicle_list.php');


       }


       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edyear->Text, 'lblyr');


       $cmbtrack = $this->cmbtrack->getItemIndex();
       if($cmbtrack == '--Select--')
          {
             $this->lbltrack->Caption = '&darr This field is required &darr';
             $this->hfError->setValue('1');
          }

       $plate = $this->edveh->Text;

       if($plate=='')
       $this->validateRequired($this->edveh->Text, 'lblplate');

       if($plate!='')
       {
       if(!preg_match("/^[a-zA-Z]{2,3}[0-9]{3,4}$/",$this->edveh->Text))
       {
        $this->lblplate->Caption = '&darr;&nbsp;Incorrect format - e.g.(ZTE168)&nbsp;&darr;';
        $this->hfError->setValue('1');
       }
       }
       /*if($this->cmbtrack->getItemIndex()=='distance')
       {
       $this->validateRequired($this->edodometer->Text, 'lblOdometer');
       }
       if($this->cmbtrack->getItemIndex()=='hours')
       {
       $this->validateRequired($this->edhours->Text, 'lblHours');
       }*/

       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $mnfr_id = $this->cmbmanufacturer->getItemIndex();
       $result_mnfr = mysql_query("SELECT * FROM lookup_vehicle_tbl WHERE company_id='".$_SESSION['company_id']."' and id='".$mnfr_id."'");
       $row_mnfr = mysql_fetch_array($result_mnfr);

       $style_id = $this->cmbtype->getItemIndex();
       $result_style = mysql_query("SELECT value FROM lookup_vehicle_tbl WHERE id='".$style_id."'") or die("ERROR STYLE ".mysql_error());
       $row_style = mysql_fetch_array($result_style);
       $style_name = $row_style['value'];

       $result_veh_id = mysql_query("SELECT MAX(vehicle_id) as max FROM dts_veh_tbl") or die("SELECT VEH ID - ".mysql_error());
       $row_veh_id = mysql_fetch_array($result_veh_id);
       $veh_id = $row_veh_id['max']+1;

       $own = $this->cmbownership->getItemIndex();
       if($own!='staff')
       {
       $owner = $this->edOwnership->Text;
       }
       if($own=='staff')
       {
       $owner = $this->cmbpersonnel->getItemIndex();
       }


       $comp_id = $_SESSION['company_id'];
       $upload = $this->uploadEmp->FileExt;
       if($upload!='')
       {
       $result = mysql_query("insert into dts_veh_tbl (VIN,
       company_id,
       manufacturer_name,
       model_name,
       style_name,
       year,
       track_by,
       tracking_value,
       curr_tracking_value,
       start_hours,
       curr_hours,
       ownership,
       customer,
       veh_num,
       engine_model,
       chassis_num,
       operator,
       location,
       department,
       custom1,
       class,
       size,
       capacity,
       other,
       photo)

       values ('".$this->edvin->Text."',
       '".$comp_id."',
       '".$row_mnfr['value']."',
       '".$this->cmbmodel->getItemIndex()."',
       '".$this->cmbtype->getItemIndex()."',
       '".$this->edyear->Text."',
       '".$this->cmbtrack->getItemIndex()."',
       '".$this->edodometer->Text."',
       '".$this->edodometer->Text."',
       '".$this->edhours->Text."',
       '".$this->edhours->Text."',
       '".$this->cmbownership->getItemIndex()."',
       '".$owner."',
       '".$this->edveh->Text."',
       '".$this->edEngine->Text."',
       '".$this->edChassis->Text."',
       '".$this->cmbvehop->getItemIndex()."',
       '".$this->cmblocation->getItemIndex()."',
       '".$this->cmbDept->getItemIndex()."',
       '".$this->Memo1->Text."',
       '".$this->cmbclass->getItemIndex()."',
       '".$this->cmbsize->getItemIndex()."',
       '".$this->edCapacity->Text."',
       '".$this->edother->Text."',
       '"."photo/vehicle/veh".$veh_id.".".$this->uploadEmp->FileExt."')")or die("INSERT VEHICLE - " . mysql_error());
       }


       if($upload=='')
       {
       $result = mysql_query("insert into dts_veh_tbl (VIN,
       company_id,
       manufacturer_name,
       model_name,
       style_name,
       year,
       track_by,
       tracking_value,
       curr_tracking_value,
       start_hours,
       curr_hours,
       ownership,
       customer,
       veh_num,
       engine_model,
       chassis_num,
       operator,
       location,
       department,
       custom1,
       class,
       size,
       capacity,
       other,
       photo)

       values ('".$this->edvin->Text."',
       '".$comp_id."',
       '".$row_mnfr['value']."',
       '".$this->cmbmodel->getItemIndex()."',
       '".$this->cmbtype->getItemIndex()."',
       '".$this->edyear->Text."',
       '".$this->cmbtrack->getItemIndex()."',
       '".$this->edodometer->Text."',
       '".$this->edodometer->Text."',
       '".$this->edhours->Text."',
       '".$this->edhours->Text."',
       '".$this->cmbownership->getItemIndex()."',
       '".$owner."',
       '".$this->edveh->Text."',
       '".$this->edEngine->Text."',
       '".$this->edChassis->Text."',
       '".$this->cmbvehop->getItemIndex()."',
       '".$this->cmblocation->getItemIndex()."',
       '".$this->cmbDept->getItemIndex()."',
       '".$this->Memo1->Text."',
       '".$this->cmbclass->getItemIndex()."',
       '".$this->cmbsize->getItemIndex()."',
       '".$this->edCapacity->Text."',
       '".$this->edother->Text."',
       'image/no_image.png')")or die("INSERT VEHICLE - " . mysql_error());
       }

       $this->uploadEmp->moveUploadedFile("photo/vehicle/veh".$veh_id.".".$this->uploadEmp->FileExt);

       //start auto-generate vin (note:INSERT STATEMENT)
       //die($this->hiddenExist->getValue());
       if($this->hiddenExist->getValue()=='0')
       {
       mysql_query("INSERT INTO lookup_dts_veh_count (company_id,total_veh_count)
       VALUES ('".$comp_id."','1')")
       or die("INSERT COUNT ".mysql_error());
       }
       if($this->hiddenExist->getValue()=='1')
       {
       mysql_query("UPDATE lookup_dts_veh_count SET total_veh_count='".$this->hiddenNewCount->getValue()."' WHERE
       company_id='".$comp_id."'")
       or die("UPDATE COUNT ".mysql_error());;
       }
       //end auto-generate vin


       //**START INSERT TEMPLATE REMINDER**

       /*$result_vehid = mysql_query("SELECT last_insert_id(vehicle_id) as new_veh_id FROM dts_veh_tbl ORDER BY vehicle_id DESC");
       $row_vehid = mysql_fetch_array($result_vehid);

       //$new_veh_id = $row_vehid['new_veh_id']+1;

       $query_template = "select * from template_recurring_tbl where company_id='".$_SESSION['company_id']."' and (style_id='".$style_id."' or style_id='All')";
       $result_template = mysql_query($query_template) or die("TEMPLATE ".mysql_error());
       $temp_count = mysql_num_rows($result_template);
       //die($query_template);
       if($temp_count!=0)
       //$date_basis =$row_vehid['computed_date'];
       while($row_template = mysql_fetch_array($result_template))
       {
       //schedule based on Time/Date
       //$basis = date('Y-m-d');
       $recurring = $row_template['recurring'];
       $template_id = $row_template['template_id'];

       $basis = $row_template['date_basis'];
       $days_mos = $row_template['due_by'];
       $val = $row_template['due_time'];

       if($recurring=='1')
       {

       if($days_mos=='Days')
       {
       $ctr_day = '0';
       $date = date($basis);


       do
       {
       $val_mul = $val * $ctr_day;
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val_mul." day");
       $due_date = date("Y-m-d", $date_computed);
       //die($val_mul."-".$val."-".$ctr_day."-".$date."-".$due_date);
       $ctr_day++;
       }
       while(date('Y-m-d')>$due_date);

       //die($val_mul."-".$val."-".$ctr_day."-".$date."-".$due_date);
       }

       if($days_mos=='Months')
       {
       $ctr_month = '0';
       $date = date($basis);
       do
       {
       $val_mul = $val * $ctr_month;
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val_mul." month");
       $due_date = date("Y-m-d", $date_computed);
       $ctr_month++;
       }
       while(date('Y-m-d')>$due_date);
       }


       //schedule base on Usage
       $curr_od = $this->edodometer->Text;
       $due_od = $row_template['due_usage'];

       $sched_by = $row_template['schedule_by'];

       if($sched_by=="Mileage" || $sched_by=="Hours")
       {
       $due = $row_template['usage_basis'];
       while($curr_od>$due)
       {
       $due+=$due_od;
       }
       $due_usage = $due;
       }
       elseif($sched_by=="Time")
       {
       $due_usage = '0';
       }
       //end schedule base on Usage


       }
       if($recurring=='0')
       {
        $due_date = date($basis);
        $due_usage = $row_template['usage_basis'];
       }

       $sched_by = $row_template['schedule_by'];

       if($sched_by=="Time" || $sched_by=="Both")
       {
       $date_due = $due_date;
       }

       if($sched_by=="Usage" || $sched_by=="Mileage" || $sched_by=="Hours")
       {
       $date_due = '0000-00-00';
       }
       //end schedule based on Time/Date



       if($sched_by=='Mileage' || $sched_by=='Hours')
       {
         $save_rem = "rem_hours,";
         $save_rem2 = "'".$row_template['rem_usage']."',";
       }
       if($sched_by=='Time')
       {
         $save_rem = "rem_by,rem_time,";
         $save_rem2 = "'".$row_template['rem_by']."','".$row_template['rem_time']."',";
       }

       if($sched_by=='Mileage' || $sched_by=='Hours')
       {
         $save_every = "every_hours,";
         $save_every2 = "'".$row_template['due_usage']."',";
       }
       if($sched_by=='Time')
       {
         $save_every = "every_by,every_date,";
         $save_every2 = "'".$row_template['due_by']."','".$row_template['due_time']."',";
       }

       //10/18/2010 - changed flag_temp to recur_per_veh
       $query_ins_remin = "insert into veh_reminder_tbl (company_id,
       vehicle_id,
       schedule_by,
       due_hours,
       due_date,
       ".$save_rem."
       ".$save_every."
       completion_window,
       type,
       description,
       price,
       status2,
       recur_per_veh,
       recurring_id,
       template_id)

       values ('".$_SESSION['company_id']."',
       '".$row_vehid['new_veh_id']."',
       '".$row_template['schedule_by']."',
       '".$due_usage."',
       '".$date_due."',
       ".$save_rem2."
       ".$save_every2."
       '".$row_template['completion_window']."',
       '".$row_template['type']."',
       '".$row_template['description']."',
       '".$row_template['price']."',
       'Pending',
       '".$recurring."',
       '".$row_template['id']."',
       '".$template_id."')";
       //die($query_ins_remin);
       mysql_query($query_ins_remin) or die("INSERT REMINDER - " . mysql_error());
       }
       //**END INSERT TEMPLATE REMINDER**
       $this->insertLTO();*/

       echo"<script language=javascript>
       var answer = confirm('Click OK to add another record or CANCEL to go back to list');
       if(answer)
       {
       window.location = 'vehicle_create.php';
       }
       else
       {
       window.location = 'vehicle_list.php';
       }
       </script>";

       //redirect('vehicle_list.php');
       }
       }

       function insertLTO()
       {
        $result_veh_id = mysql_query("SELECT last_insert_id(vehicle_id) as veh_id FROM `dts_veh_tbl` ORDER BY vehicle_id DESC LIMIT 0,1");
        $row_veh_id = mysql_fetch_array($result_veh_id);

        $last_number_plate = substr($this->edveh->Text,5,1);
        if($last_number_plate!=0)
        {
         $month = '0'.$last_number_plate;
        }
        else
        {
         $month = '10';
        }

        $year = date('Y');
        $day = '01';

        $due_date = $year.'-'.$month.'-'.$day;
        if($due_date<date('Y-m-d'))
        {
         $date_basis = date($due_date);
         $temp_date = strtotime(date("Y-m-d", strtotime($date_basis)) . "+12 month");
         $due_date = date("Y-m-d",$temp_date);
        }

        $insert_recurring = "insert into veh_reminder_tbl (company_id,
        vehicle_id,
        recur_per_veh,
        schedule_by,
        due_hours,
        due_date,
        every_hours,
        every_date,
        every_by,
        rem_by,
        rem_time,
        completion_window,
        type,
        description,
        vendor,
        comments,
        status,
        status2)

        values('".$_SESSION['company_id']."',
        '".$row_veh_id['veh_id']."',
        '1',
        'Date',
        '',
        '".$due_date."',
        '',
        '12',
        'Months',
        'Months',
        '1',
        'None',
        'Payment',
        'LTO Registration',
        '',
        'LTO Registration',
        '',
        'Pending')";

        mysql_query($insert_recurring)or die("INSERT REMINDER RECURRING - " . mysql_error());
       }

       function cmbmaintenanceChange($sender, $params)
       {
       $maintenance = $this->cmbmaintenance->getItemIndex();
       $this->cmbmaintenance2->Clear();
       if ($maintenance=="in house")
       {
        $result5 = mysql_query("select lname from employee_tbl where job_title='fleet technician'");

        while($row5 = mysql_fetch_array($result5))
        {
            $this->cmbmaintenance2->AddItem($row5['lname'], null, $row4['lname']);
        }
       }
       else
       {
        $result6 = mysql_query("select vendor_name from vendor_tbl");

        while($row6 = mysql_fetch_array($result6))
        {
            $this->cmbmaintenance2->AddItem($row6['vendor_name'], null, $row6['vendor_name']);
        }
       }



       }


       function cmbmanufacturerChange($sender, $params)
       {

       $mnfr_id = $this->cmbmanufacturer->getItemIndex();
       $this->cmbmodel->Clear();
       $result4 = mysql_query("select model_name from lookup_models_tbl where company_id='".$_SESSION['company_id']."' and mnfr_id='".$mnfr_id."'");

       while($row4 = mysql_fetch_array($result4))
       {
            $this->cmbmodel->AddItem($row4['model_name'], null, $row4['model_name']);
       }

       }



       function cmbtrackChange($sender, $params)
       {
       $tracking = $this->cmbtrack->getItemIndex();
       if ($tracking=="distance")
       {
       $this->lblOd->Caption='Odometer:';
       $this->lblCurrOd->Caption='Curr Odometer';
       $save_here = "distance";
       }
       else
       {
       $this->lblOd->Caption='Hours:';
       $this->lblCurrOd->Caption='Curr hours';
       $save_here = "hours";
       }
       }



       function size()
      {
       $size = $this->cmbsize->getItemIndex();

       if($size=='other')
       {
       $this->edother->Enabled=true;
       }

       if($size!='other')
       {
       $this->edother->Enabled=false;
       }
      }


      function owner()
      {
      $result_comp = mysql_query("select * from company_tbl where company_id = '".$_SESSION['company_id']."'");
      $row_comp = mysql_fetch_array($result_comp);
      $ownership = $this->cmbownership->getItemIndex();
      //echo "<script type=text/javascript>
        //alert($ownership);
        //</script>";
      if($ownership=='staff')
      {
      $this->cmbpersonnel->setVisible(true);
      $this->cmbpersonnel->Left = 113;
      $this->edcustomer->setVisible(true);
      $this->edcustomer->Left = 632;
      }

      if($ownership=='company')
      {
      $this->cmbpersonnel->setVisible(true);
      $this->cmbpersonnel->Left = 632;
      $this->edcustomer->setVisible(true);
      $this->edcustomer->Left = 113;
      $this->edcustomer->Text=$row_comp['company_name'];
      }

      if($ownership=='leased' || $ownership=='outsource')
      {
      $this->cmbpersonnel->setVisible(true);
      $this->cmbpersonnel->Left = 632;
      $this->edcustomer->setVisible(true);
      $this->edcustomer->Left = 113;
      $this->edcustomer->Text='';
      }
      }



      function clr()
       {
       $this->edyear->Text = '';
       $this->edcustomer->Text = '';
       $this->edveh->Text = '';
       $this->Memo1->Text = '';
       $this->edodometer->Text = '';
       $this->edCurrOd->Text = '';
       $this->edhours->Text = '';
       $this->Memo1->Lines=array();
       $this->edChassis->Text='';
       $this->edEngine->Text='';
       $this->cmbmanufacturer->setItemIndex('');
       $this->cmbclass->setItemIndex('');
       $this->cmbtrack->setItemIndex('');
       }


       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr This field is required &darr';
             $this->hfError->setValue('1');
          }
       }




       function Vehicle_CreateCreate($sender, $params)
       {
       $this->clr();

       $this->compid->Value = $_SESSION['company_id'];
       $comp_id = $_SESSION['company_id'];
       $this->hiddenvehnum->setValue($_GET['vehicle_id']);
       $this->hfError->setValue('0');

       $this->lblyr->Caption = '';
       $this->lblplate->Caption = '';
       $this->lblOdometer->Caption = '';
       $this->lblHours->Caption = '';
       $this->lbltrack->Caption = '';
       $this->Memo1->Lines=array();

       $result_comp = mysql_query("select * from company_tbl where company_id = '".$_SESSION['company_id']."'");
       $row_comp = mysql_fetch_array($result_comp);
       $this->hiddenCompanyName->setValue($row_comp['company_name']);

       //LIMIT VEHICLES
       $query_veh_count = "SELECT COUNT(vehicle_id) AS veh_count FROM dts_veh_tbl WHERE company_id='".$_SESSION['company_id']."'";
       $result_veh_count = mysql_query($query_veh_count) or die("VEH COUNT ".mysql_error());
       $row_veh_count = mysql_fetch_array($result_veh_count);

       $veh_count = $row_veh_count['veh_count'];
       $veh_limit = $row_comp['veh_limit'];

       if($veh_count>=$veh_limit && $veh_limit!='0')
       {
        echo "<script type='text/javascript'>
        alert('You have reached your maximum number of vehicles!');
        document.location.href = 'vehicle_list.php';
        </script>";
       }


       //start auto-generate vin
       //hiddenExist
       //hiddenNewCount
       //$concat_string = 'VIN';
       $result_veh_count = mysql_query("SELECT * FROM lookup_dts_veh_count WHERE company_id='".$_SESSION['company_id']."'")
       or die("vehicle count ".mysql_error());
       $row_veh_count = mysql_fetch_array($result_veh_count);
       $concat_string = $row_veh_count['prefix'];

       if(mysql_num_rows($result_veh_count)==0)
       {
        $this->edvin->Text = $concat_string.'000001';
        $this->hiddenExist->setValue('0');
       }
       if(mysql_num_rows($result_veh_count)>0)
       {

        $number = $row_veh_count['total_veh_count']+1;

        $this->hiddenNewCount->setValue($number);

        $length = strlen($number);
        $num_zero = 6 - $length;
        $zero = '';
        while($num_zero>0)
         {
           $zero = $zero.'0';
           $num_zero--;
         }
        $this->edvin->Text = $concat_string.$zero.$number;
        $this->hiddenExist->setValue('1');
       }
       //end auto-generate vin

       //COMBO BOX
       //$this->cmbmanufacturer->setItemIndex('');
       //$this->cmbtrack->setItemIndex('');
       //$this->cmbmodel->Clear();
       $this->cmbownership->setItemIndex('staff owned');


       $this->cmbmanufacturer->Clear();
       //$this->cmbmanufacturer->AddItem('--Select--', null, '--Select--');
       $result_mnfr = mysql_query("select * from lookup_vehicle_tbl where company_id='".$_SESSION['company_id']."' and category='manufacturer' order by value asc");

       while($row_mnfr = mysql_fetch_array($result_mnfr))
        {
            $this->cmbmanufacturer->AddItem($row_mnfr['value'], null, $row_mnfr['id']);
        }

        /*$this->cmbtype->Clear();
        $result_style = mysql_query("select * from lookup_vehicle_tbl where company_id='".$_SESSION['company_id']."' AND category='body styles' order by value asc");

        while($row_style = mysql_fetch_array($result_style))
        {
            $this->cmbtype->AddItem($row_style['value'], null, $row_style['id']);
        }*/

         //list type
        $this->listType->Clear();
        $result_type = mysql_query("select * from lookup_vehicle_tbl where company_id='".$_SESSION['company_id']."' AND category='body styles' order by value asc");


        while($row_type = mysql_fetch_array($result_type))
        {
            $this->listType->AddItem($row_type['value'], null, $row_type['id']);
        }


        $this->cmbpersonnel->Clear();
        $result_personnel = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."'");

        while($row_personnel = mysql_fetch_array($result_personnel))
        {
            $this->cmbpersonnel->AddItem($row_personnel['lname'].','.$row_personnel['fname'], null, $row_personnel['lname'].','.$row_personnel['fname']);
        }


        /*$this->cmbownership->Clear();
        $result_ownership = mysql_query("select * from lookup_vehicle_tbl where company_id = '".$_SESSION['company_id']."' and category='ownership'");

        while($row_ownership = mysql_fetch_array($result_ownership))
        {
            $this->cmbownership->AddItem($row_ownership['value'], null, $row_ownership['value']);
        }*/

        $this->listOwnership->Clear();
        $result_owner = mysql_query("select * from lookup_vehicle_tbl where company_id = '".$_SESSION['company_id']."' and category='ownership'");

        while($row_owner = mysql_fetch_array($result_owner))
        {
            $this->listOwnership->AddItem($row_owner['value'], null, $row_owner['value']);
        }



        $this->cmbvehop->Clear();
        $result_vehop = mysql_query("select * from employee_tbl where veh_op='1' and company_id='".$_SESSION['company_id']."'");

        while($row_vehop = mysql_fetch_array($result_vehop))
        {
            $this->cmbvehop->AddItem($row_vehop['lname'].','.$row_vehop['fname'], null, $row_vehop['lname'].','.$row_vehop['fname']);
        }



        /*$this->cmblocation->Clear();
        $result_loc = mysql_query("select value from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='work locations'");

        while($row_loc = mysql_fetch_array($result_loc))
        {
            $this->cmblocation->AddItem($row_loc['value'], null, $row_loc['value']);
        }*/

        $this->cmblocation->Clear();
        $this->cmblocation->AddItem('--Select--', null, '--Select--');
        $result_loc = mysql_query("select * from company_garage where company_id='".$_SESSION['company_id']."'");

        while($row_loc = mysql_fetch_array($result_loc))
        {
            $this->cmblocation->AddItem($row_loc['name'], null, $row_loc['name']);
        }

        //list garage
        $this->listGarage->Clear();
        $result_garage = mysql_query("select value from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='work locations'");

        while($row_garage = mysql_fetch_array($result_garage))
        {
            $this->listGarage->AddItem($row_garage['value'], null, $row_garage['value']);
        }

        $this->cmbDept->Clear();
        $this->cmbDept->AddItem('--Select--', null, '--Select--');
        $result_department = mysql_query("select * from lookup_personnel_tbl where company_id = '".$_SESSION['company_id']."' AND category='departments'");

        while($row_department = mysql_fetch_array($result_department))
        {
            $this->cmbDept->AddItem($row_department['value'], null, $row_department['id']);
        }

        //list department
        $this->listDept->Clear();
        $result_dept = mysql_query("select * from lookup_personnel_tbl where company_id = '".$_SESSION['company_id']."' AND category='departments'");

        while($row_dept = mysql_fetch_array($result_dept))
        {
            $this->listDept->AddItem($row_dept['value'], null, $row_dept['id']);
        }


        /*$result = mysql_query("select employee_tbl.emp_id as employee_id,
        employee_tbl.lname as lname,
        employee_tbl.fname as fname
        from employee_tbl
        inner join emp_license_tbl
        on employee_tbl.emp_id = emp_license_tbl.emp_id
        where emp_license_tbl.vehicle_op='1'");

       while($row = mysql_fetch_array($result))
        {
            $this->cmbvehop->AddItem($row['lname'].",".$row['fname'], null, $row['lname'].",".$row['fname']);
        }*/

       //$this->edvin->Text = '';


       $this->imgBack->setLink('vehicle_list.php?company_id='.$_SESSION['company_id']);

       }


}

global $application;

global $Vehicle_Create;
//Creates the form
$Vehicle_Create=new Vehicle_Create($application);

//Read from resource file
$Vehicle_Create->loadResource(__FILE__);

//Shows the form
$Vehicle_Create->show();

?>